/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rgenerator;

import java.io.File;
import java.util.ArrayList;

/**
 *
 * @author alex2stf <alex2stf@yahoo.com>
 */
public class PackageMaker {
    
    private String className;
    private ArrayList<String> listaDeNumeDeVariabile=new ArrayList<>();
    private int uniqueIndex=0;
    private ArrayList<String> listaDeVariabile;
    
    private String introName;
    private String prefix;
    private boolean isR=false;
    private Stringers type;
   

    public String getFileName() {
        if (isR) {
            return className+".as"; 
        }
        return className+prefix+".as";
    }
    

    public ArrayList<String> getListaDeVariabile() {
        return listaDeVariabile;
    }
    
    
  
    
    
    public PackageMaker(String classNameParam, String packageName, String mainFileName, String classNamePrefix,  Stringers type){
    this.type=type;
    this.prefix=classNamePrefix;
    this.className=Stringers.makeSafe(classNameParam);
    listaDeVariabile =new ArrayList<>();
    
    listaDeVariabile.add(" /* autogenerated file created by ClassFileR 1.0 */ ");
    listaDeVariabile.add("");
    
    String pack="package "+packageName+"{";
    listaDeVariabile.add(pack);
    //System.out.println(pack);
     
   //pune numele clasei
    String start;
        
        // daca e main
        if (className.equals(mainFileName)) { 
               isR=true;
               
               if (type.isGetterMode()) {
                   introName="       public var ";
               } 
               
               else{
                   introName="       public static const ";
               }
               
               start=" public class "+className+"{";
        }
        
        //daca nu e main
        else{
              isR=false;
              if(type.isGetterMode()){
                  introName="       public function get ";
              }
              else{
                  introName="       public var ";
              }
              start=" public class "+className+prefix+"{";
        }
     
    listaDeVariabile.add(start);
    System.out.println(start);
    
    }
    
    
    
            
    
    public void addVars(String numeVariabila, String valoare, String numeIntreg){  
       
        //daca vrea sa pastreze extensia:
//        if (type.isEnableExtensionMode()) {
//                numeVariabila=numeIntreg.replaceAll("[.]", "_");
//            }
        
        
        //ne asiguram ca numele dat nu e in lista, si ii punem numele cu extensia
        if (isCheckUnique(listaDeNumeDeVariabile, numeVariabila)) {
            uniqueIndex++;
            numeVariabila=numeIntreg.replaceAll("[.]", "_");
        }
        
        numeVariabila=Stringers.makeSafe(numeVariabila);
        
        if (numeVariabila.equals(className+prefix)) {
            numeVariabila+="_var";
        }
        
        //numele de variabila e mereu lowerCase: ??? wtf???
        //numeVariabila=numeVariabila.toLowerCase();
        if (type.slashInFront) {
            valoare="/"+valoare;
        } 
        
        if (type.doubleSlashed) {
            valoare=valoare.replaceAll("/", "//");
        }
        
//        if (type.backSlashed) {
//            valoare=valoare.replaceAll("/", "\\");
//        }
        
        
        
        //il punem in lista interna dupa verificare:
        listaDeNumeDeVariabile.add(numeVariabila);
        
        //il punem in lista de outPut:
        if (type.isGetterMode()) {
            String var="       public function get "+numeVariabila+"():String { return \"" +valoare+"\"; }";  
            listaDeVariabile.add(var);
        }
        else{
            String var=introName+numeVariabila+":String = \"" +valoare+"\";";  
            listaDeVariabile.add(var);
        }
        
        
    }
    
    
    
    //verifica daca e unic in lista numele de clasa generat din numele de folder
    private static Boolean isCheckUnique(ArrayList<String> paramList, String param ){
        for (int i = 0; i < paramList.size(); i++) {
            if (paramList.get(i).equals(param)) {
                return true;
            }
        }
        return false;
    }
    
    
    
    public void close(File fisiere[], String caleAbsoluta){ 
            for (int i = 0; i < fisiere.length; i++) {
                if (fisiere[i].isDirectory()) {
                    
                    
                    String tipDeClasa;
                    String caleTemporara=fisiere[i].toURI().toString();
                    String caleRelativa=caleTemporara.replaceAll(caleAbsoluta, "");
                    tipDeClasa=Stringers.convertPathToClassName(caleRelativa);
                    
                    
                    //numele variabilei:
                    String numeVar=fisiere[i].getName();
                    
                    //facem safe denumirile:
                    numeVar=Stringers.makeSafe(numeVar);
                    tipDeClasa=Stringers.makeSafe(tipDeClasa);
                    
                    if (type.isGetterMode()) {
                        String classVar="       public var "+numeVar+":"+tipDeClasa+prefix+"= new "+tipDeClasa+prefix+"();";
                        listaDeVariabile.add(classVar);
                    }
                    else{
                        String classVar=introName+numeVar+":"+tipDeClasa+prefix+"= new "+tipDeClasa+prefix+"();";
                        listaDeVariabile.add(classVar);
                    }
                    
                    
                    
                }//exit if
            }//exit for
            
            String fin=" }";
            String finPack="}";
            listaDeVariabile.add(fin); 
            listaDeVariabile.add(finPack);
    }
    
}
